package com.yanbao.properties;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.*;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
@Configuration
@PropertySource("classpath:shiro.properties")
@ConfigurationProperties(prefix = ShiroProperties.SHIRO_PREFIX)
public class ShiroProperties {

    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    public static final String SHIRO_PREFIX = "shiro";

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/

    /**
     * 默认的loginurl
     * LoginUrl
     */
    private String configLoginUrl;

    /**
     * 默认登录成功跳转的url
     * LoginSuccessUrl
     */
    private String configLoginSuccesUrl;

    /**
     * 需要调用的登录的url
     */
    private String loginUrls;

    /**
     * SessionID
     */
    private String sessionId;

    /**
     * SesssionDomain
     */
    private String sessionDomain;

    /**
     * SessionAge
     */
    private String sessionAge;

    /**
     * 需要权限的url
     */
    private List<String> auths = new ArrayList<>();

    private String pageSuffix;

    private String ajaxSuffix;

    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    public String getConfigLoginUrl() {
        return configLoginUrl;
    }

    public void setConfigLoginUrl(String configLoginUrl) {
        this.configLoginUrl = configLoginUrl;
    }

    public String getConfigLoginSuccesUrl() {
        return configLoginSuccesUrl;
    }

    public void setConfigLoginSuccesUrl(String configLoginSuccesUrl) {
        this.configLoginSuccesUrl = configLoginSuccesUrl;
    }

    public String getLoginUrls() {
        return loginUrls;
    }

    public void setLoginUrls(String loginUrls) {
        this.loginUrls = loginUrls;
    }

    public List<String> parseLoginUrlToList(){
        if(StringUtils.isNotEmpty(loginUrls)){
            return Arrays.asList(StringUtils.split(loginUrls,","));
        }
        return null;
    }

    public List<String> getAuths() {
        return auths;
    }

    public void setAuths(List<String> auths) {
        this.auths = auths;
    }

    public Map<String,String> parseAuthsToMap(){
        if(auths!=null){
            Map<String,String> authMap = new HashMap<>();
            (new ArrayList<>(auths)).forEach(x->{
                String[] arr = StringUtils.split(x,",");
                authMap.put(arr[0],arr[1]);
            });
            return authMap;
        }
        return null;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getSessionDomain() {
        return sessionDomain;
    }

    public void setSessionDomain(String sessionDomain) {
        this.sessionDomain = sessionDomain;
    }

    public String getSessionAge() {
        return sessionAge;
    }

    public void setSessionAge(String sessionAge) {
        this.sessionAge = sessionAge;
    }

    public String getPageSuffix() {
        return pageSuffix;
    }

    public void setPageSuffix(String pageSuffix) {
        this.pageSuffix = pageSuffix;
    }

    public String getAjaxSuffix() {
        return ajaxSuffix;
    }

    public void setAjaxSuffix(String ajaxSuffix) {
        this.ajaxSuffix = ajaxSuffix;
    }

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
